package registnet.records.source;

import grammar.PrerequisiteCruncher;

import java.util.List;
import java.util.Vector;

import registnet.records.Curriculum;
import registnet.records.CurriculumCourse;
import registnet.records.Department;
import registnet.records.IPrerequisite;
import registnet.records.PlanOfCoursework;
import registnet.records.Prerequisite;
import registnet.records.Student;

public class PlanOfCourseworkSource {

	private Student student;
	private PlanOfCoursework planOfCoursework;
	
	
	/**
	 * Default constructor
	 * 
	 * @param student - The student who owns the plan of coursework
	 */
	public PlanOfCourseworkSource(Student student){
		this.student = student;
		this.load();
	}
	
	
	/**
	 * Loads data from source
	 */
	public void load(){
		try{
			registnet.dao.Student DAOStudent = new registnet.dao.StudentPeer().getStudent(this.student.getStudentNumber());
			List<registnet.dao.PlanOfCourseWork> DAOPlanOfCourseworks = new registnet.dao.PlanOfCourseWorkPeer().getPlanOfCourseWorkByStudent(DAOStudent.getStudentId());
			this.planOfCoursework = new PlanOfCoursework(this.student);
			for(int i=0; i<DAOPlanOfCourseworks.size(); i++){
				registnet.dao.PlanOfCourseWork DAOPlanOfCoursework = DAOPlanOfCourseworks.get(i);
				registnet.dao.Course DAOCourse = DAOPlanOfCoursework.getCourse();
				Curriculum curriculum = new CurriculumSource(DAOStudent.getCurriculum().getCurriculumCode()).getCurriculum();
				Department department = new DepartmentSource(DAOCourse.getUnit().getUnitCode()).getDepartment();
				CurriculumCourse cc = new CurriculumCourse(DAOCourse.getCourseCode(), curriculum, DAOPlanOfCoursework.getSemester(), DAOPlanOfCoursework.getYear());
				cc.setCourseName(DAOCourse.getCourseName());
				cc.setDepartment(department);
				cc.setPrerequisites(this.getPrerequisites(DAOCourse.getPrerequisite()));
				cc.setUnit(DAOCourse.getUnits());
				this.planOfCoursework.addCourse(DAOPlanOfCoursework.getType(), cc);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Gets the prerequisites of the course
	 * 
	 * @param source - The prerequisites of the course, in text
	 * @return	The prerequisites of the course, in list
	 */
	private List<IPrerequisite> getPrerequisites(String source){
		List<IPrerequisite> prerequisites = new Vector<IPrerequisite>();
		List<String> temp = PrerequisiteCruncher.getPrerequisites(source);
		for(int i=0; i<temp.size(); i++){
			Prerequisite prerequisite = new Prerequisite(temp.get(i));
			prerequisites.add(prerequisite);
		}
		return prerequisites;
	}
	
	
	/**
	 * Gets the plan of coursework
	 * @return	The plan of coursework
	 */
	public PlanOfCoursework getPlanOfCoursework() {
		return this.planOfCoursework;
	}
}
